<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/1/13
 * Time: 9:48

  *     违章信息查询类
  *
  *
  */

namespace Modules\Api\Http\Controllers;

use Illuminate\Support\Facades\Request;

class TrafficController extends BaseController
{
    /**
     *  通过用户的信息查询违章信息
     *  hinq 2020 1 9
     *
     **/
    public function traffic_store( Request $request ){
        $arr_input = (array) $request::all();

        $url = jisuapi_api_url_illegal();


        $frameno = $arr_input['frameno'];    //  车架号 根据管局需要输入
        //获取交管局信息  根据车管局信息获取车架号的长度
        $result = self::entrust_carorg();
        foreach ( $result['data'] as $key => $vo ){
            if( $arr_input['lsprefix'] == $vo['lsprefix'] ){   //根据车牌前缀 获取当前省份信息
                if( $vo['frameno'] == 0 ){
                    $frameno = '';
                }elseif ( $vo['frameno'] == 100 ){
                    $frameno = $arr_input['frameno'];    //  车架号 根据管局需要输入
                }else{
                    $frameno = substr( $arr_input['frameno']  , -$vo['frameno']  );
                }
            }
        }

        $post = [
            'lsprefix'=>$arr_input['lsprefix'],   //  车牌前缀( 京、沪、皖....)
            'lsnum'=>$arr_input['lsnum'],         //  车牌剩余部分 （A123456）
            'engineno'=>$arr_input['engineno'],   //  发动机号 根据管局需要输入
            'frameno'=>$frameno,     //  车架号 根据管局需要输入
            'lstype'=>empty( $arr_input['lstype'] ) ? '02' : $arr_input['lstype']        //  车辆类型 默认小车02
        ];

        //这里是正式请求接口的数据
        $result = curl_post( $url, $post );
        $jsonarr = json_decode($result, true);   //将返回的数据转成数组格式

        if($jsonarr['status'] != 0)
        {

            return hello_error($jsonarr['msg'] , null);
        }
        $result = $jsonarr['result'];
        if(!empty($result['list']))  //有违法信息列表
        {
            $temp['list'] = $result['list'];
            foreach ( $result['list'] as $key=>$vo ){
                $temp['lsprefix'] = $vo['lsprefix'];
                $temp['lsnum'] = $vo['lsnum'];
                $temp['carorg'] = $vo['carorg'];
                $temp['usercarid'] = $vo['usercarid'];
                if( $vo['canhandle'] == 1 && $vo['score'] == 0 ){    //可以代办  并且扣分为0的才可以平台代办
                    $temp['canhandle'][] = $vo;
                }else if( $vo['canhandle'] == 1 && $vo['score'] > 0 ) {  //可以代办  并且扣分不为0的平台不可以代办
                    $temp['nocanhandle'][] = $vo;
                }else{
                    $temp['fees'][] = $vo;
                }
            }
        } else {
            $temp['msg'] = '恭喜你 ， 没有违章信息';
            $temp['list'] = [];
        }
        return hello_success('ok' , $temp );
    }

    /*违章统计数据*/
    public function traffic_store_total( Request $request ){
        $arr_input = (array) $request::all();
        if(mb_strlen($arr_input['frameno']) <= 0){
            return hello_error('请输入车架号');
        }
        if(mb_strlen($arr_input['lsprefix']) <= 0){
            return hello_error('请输入车牌省份');
        }
        if(mb_strlen($arr_input['lsnum']) <= 0){
            return hello_error('请输入车牌号剩余部分');
        }
        if(mb_strlen($arr_input['engineno']) <= 0){
            return hello_error('请输入发动机号');
        }

        //获取交管局信息
        $s_redis_key = '交管局_全国';
        $re = predis_str_get($s_redis_key);
        if(!is_null($re)){
            $arr_carorg = $re;

        }
        else{
            $url_carorg = jisuapi_api_url_carorg();
//            $result = curlOpen( $url_carorg , ['ssl'=>true]);   //通过接口获取全国交管所的数据
            $result = curl_get( $url_carorg);   //通过接口获取全国交管所的数据
            $result = json_decode($result, true);

            if($result['status'] == 0){
                $arr_carorg = $result['result']['data'];
                $n_time = 24 * 60 *60;
                predis_str_set($s_redis_key,$arr_carorg,$n_time);
            }
            else{
                return hello_error($result['msg']);
            }
        }


        $s_full_plate_no = $arr_input['lsprefix'].$arr_input['lsnum'];
        $s_redis_key = '违章查询_'.$s_full_plate_no;
        $re = predis_str_get($s_redis_key);
        if(!is_null($re)){
            $arr_data = $re;
        }
        else{
            function substr_params($s_str,$n_length){
                if($n_length === 0){
                    $str = '';
                }
                elseif ($n_length === 6){
                    $str = substr($s_str,-6);
                }
                else{
                    $str = $s_str;
                }
                return $str;
            }

            //处理车架号和发动机号 根据管局需要输入
            $result = collect($arr_carorg)->where('lsprefix',$arr_input['lsprefix'])->first();
            $arr_input['frameno'] = substr_params($arr_input['frameno'],$result['frameno']);
            $arr_input['engineno'] = substr_params($arr_input['engineno'],$result['engineno']);
            $arr_input = array_remain($arr_input,['lsprefix','lsnum','engineno','frameno','lstype']);

            //查询违章信息-统计
            $url_illegal = jisuapi_api_url_illegal();
            $result = curl_post( $url_illegal, $arr_input );
            $result = json_decode($result, true);   //将返回的数据转成数组格式
            $n_time = 2 * 60 *60;

            if($result['status'] == 0){
                $arr_data = [
                  'illegalnum'=>$result['result']['count'],
                  'othernum'=>0,
                  'localnum'=>0,
                ];
                predis_str_set($s_redis_key,$arr_data,$n_time);//缓存数据

            }
            elseif($result['status'] == 217){
                $arr_data = $result['result'];
                predis_str_set($s_redis_key,$arr_data,$n_time);//缓存数据
            }
            else{
                return hello_error($result['msg'],[]);
            }

        }

        return  hello_success('查询成功',$arr_data);

    }


    /**
     *  通过违章id获取详细信息
     *  hinq 2020 1 10
     *
     **/
    public function traffic_entrust(){
        return view('admin::traffic.entrust' );
    }

    /**
     *  通过违章id获取详细信息
     *  hinq 2020 1 10
     **/
    public function entrust_store(  Request $request  ){
        $arr_input = (array) $request::all();
        $illegalid = '340020191210278328';//违章ID  多个用逗号隔开
        $mobile = '';
        $outorderno = '';
        $appsecret = config('system.jssj_app_secret');
        $queryarr = array('mobile'=>$mobile,'illegalid'=>$illegalid,'outorderno'=>$outorderno);
        $sign = makeSign($queryarr, $appsecret);   //MD5 生成签名
        $url =  jisuapi_api_url_entrust()."&mobile=$mobile&illegalid=$illegalid&outorderno=$outorderno&sign=".$sign;
//        $result = curlOpen($url, ['ssl'=>true]);
//        $jsonarr = json_decode($result, true);
//        if($jsonarr['status'] != 0)
//        {
//            return hello_error($jsonarr['msg']);
//        }

        $data = [
            "mobile"=> "",      //手机号
            "illegalid"=> "1333,1334",      //违章ID ,分割
            "outorderno"=> "",      //商家订单号
            "orderno"=> "201604280717555074534",        //订单号
            "totalfee"=> "438",     //总金额
            "servicefee"=> "38",        //代办费
            "lsprefix"=> "沪",       //车牌前缀
            "lsnum"=> "A11111",     //车牌号剩余部分
            "handlestatus"=> "0"   //处理状态 0处理中 1处理成功 2处理失败
        ];
        return hello_success('ok', $data );
    }

    /**
     *  分页获取所有代交列表
     *  hinq 2020 1 10
     *
     **/
    public function entrust_list( Request $request ){
        $arr_list = $request::all();
        $pagenum = empty( $arr_list['pagenum'] ) ? 1 : $arr_list['pagenum'];
        $pagesize = empty( $arr_list['pagesize'] ) ? 10 : $arr_list['pagesize'];
        $url =  jisuapi_api_url_entrust_list()."&pagenum=$pagenum&pagesize=$pagesize";
        $result = curlOpen($url, ['ssl'=>true]);
        $jsonarr = json_decode($result, true);

        $temp = [
            [
                "usercarid" => "37924995",
                "servicefee" => "38.00",
                "totalfee" => "0.01",
                "mobile" =>  "",
                "addtime" =>  "1493016827",
                "orderno" =>  "201704241453475119125r",
                "outorderno" => "",
                "status" => 0,
                "handlestatus" => "0"
            ],[
                "usercarid" => "37924996",
                "servicefee" => "65.00",
                "totalfee" => "0.01",
                "mobile" =>  "",
                "addtime" =>  "1493016659",
                "orderno" =>  "2017042414534751195654",
                "outorderno" => "",
                "status" => 0,
                "handlestatus" => "0"
            ]
        ];

        if( $jsonarr['status'] != 0 ){
            return hello_error($jsonarr['msg']);
        }else{  //光阴似箭  时光冉冉   赋文
            $data = [
                'page_data'=>[
                    'total'=> $jsonarr['result']['total'],
                    'pagesize'=>$jsonarr['result']['pagesize'],
                    'pagenum'=>$jsonarr['result']['pagenum'],
                ],
                'data' =>$temp
            ];
            return hello_success('ok' , $data );
        }
    }


    /**
     *  获取全国的交管局
     *  hinq 2020 4 7
     *  每天获取一次  获取到的信息保存到缓存中
     **/
    public function entrust_carorg(){

        $cache_entrust_carorg = \Cache::get('cache_entrust_carorg');
        if( empty( $cache_entrust_carorg  ) ){
            $url = jisuapi_api_url_carorg();
            $result = curlOpen( $url , ['ssl'=>true]);   //通过接口获取全国交管所的数据
            $jsonarr = json_decode($result, true);
            //                   当天时间的最后一秒  -  当前时间
            $time = strtotime( date('Y-m-d 23:59:59') ) - time();  //需要缓存的时间
            \Cache::set( 'cache_entrust_carorg', $jsonarr['result']['data'] , $time );
            return hello_success('ok' , $jsonarr['result']['data']  );
        }else{
            return hello_success('ok' , $cache_entrust_carorg );
        }

    }


}